// @flow
import React, { Component } from 'react'
import { Icon } from 'antd'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
import styles from './fileGrid.css'

type Props = {
  history: Object,
  data: Object
}

class JobWhereActions extends Component {
  props: Props
  goChildFun: Function
  constructor (props: Props) {
    super(props)
    this.goChildFun = this.goChildFun.bind(this)
  }
  goChildFun () {
    const { history, data } = this.props
    const path = data.path
    let file = `${__SERVICE_API__}analysis/files?hdfsPath=${path}&expand=false`
    universalFetch(file)
    .then((res) => res.json())
    .then((json) => {
      if (json.code !== 200000) {
        throw new Error(JSON.stringify({ code: json.code, message: json.message }))
      }
      // console.log(json.result)
      // const type = json.result.type
      history.push({
        pathname: '/analysis/job-file',
        state: {
          rowData:json.result
        } })
    })
    .catch((error) => {
      handleFetchError(error)
      console.error(error)
    })
  }
  render () {
    const { data } = this.props
    const { name, type } = data
    if (type === 0) {
      return (
        <span>
          <a onClick={this.goChildFun} className={styles['grid_name']}>
            <Icon type='folder' />{name}
          </a>
        </span>
      )
    } else {
      return (
        <span>
          <Icon type='file' style={{ marginRight:5 }} />{name}
        </span>
      )
    }
  }
}

export default JobWhereActions
